package com.lfnyxy.web;

import com.lfnyxy.common.LogTool;
import com.lfnyxy.common.dto.ApiResult;
import com.lfnyxy.common.dto.CodeMessage;
import com.lfnyxy.common.dto.PagedDataTable;
import com.lfnyxy.entity.DeptRefEntity;
import com.lfnyxy.query.DeptRefQuery;
import com.lfnyxy.service.DeptRefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** * Created by. */@Controller@RequestMapping("/deptRef")@ResponseBody public class DeptRefController {    @Autowired    DeptRefService deptRefService;    @RequestMapping(value = "/save", method = RequestMethod.POST)    @ResponseBody    public ApiResult<Integer> CreateDeptRef(@RequestBody DeptRefEntity item){		int result  = 0;				if(item != null){			try{                if(item.getId() > 0){                    result =  deptRefService.UpdateItem(item);                 }else{                    result =  deptRefService.CreateItem(item);                 }            }catch (Exception ex){				LogTool.WriteError(this.getClass().toString()+ "001", ex.getMessage());				return ApiResult.error(CodeMessage.SAVE_FAILED);			}        }				return ApiResult.success(result);    }    @RequestMapping(value = { "/plist" }, method = { RequestMethod.GET })    @ResponseBody    public PagedDataTable QueryPageList(@RequestParam(value = "start", required = false) int start,                                        @RequestParam(value = "length", required = false) int length,                                        @RequestParam(value = "matchString", required = false) String matchString) {        List<DeptRefEntity> result = null;        DeptRefQuery query = new DeptRefQuery();        query.setStart(start);        query.setLength(length);        query.setMatchString(matchString);        try {            result = deptRefService.QueryPageList(query);        } catch (Exception ex) {			LogTool.WriteError(this.getClass().toString()+ "002", ex.getMessage());        }        return new PagedDataTable<DeptRefEntity>(query.getTotal(), result);    }    @RequestMapping(value = { "/item" }, method = { RequestMethod.POST })    @ResponseBody    public ApiResult<DeptRefEntity> QueryItem(@RequestBody DeptRefQuery query) {         if(query != null && query.getId() > 0){            try {                DeptRefEntity result = deptRefService.QueryItem(query.getId());				return ApiResult.success(result);            } catch (Exception ex) {				LogTool.WriteError(this.getClass().toString()+ "003", ex.getMessage());            }        }		return ApiResult.error(CodeMessage.GET_ITEM_FAILED);    }    @RequestMapping(value = {"/ditem"}, method = {RequestMethod.POST})    @ResponseBody    public ApiResult<Integer> DeleteItem(@RequestBody DeptRefQuery query) {        try {            if (query != null && query.getId()> 0) {                int cnt = deptRefService.DeleteItem(query.getId());                return ApiResult.success(cnt);            }        } catch (Exception ex) {			LogTool.WriteError(this.getClass().toString()+ "004", ex.getMessage());        }        return ApiResult.error(CodeMessage.DELETE_ITEM_FAILED);    }    @RequestMapping(value = { "/all" }, method = { RequestMethod.POST })    @ResponseBody    public ApiResult<List<DeptRefEntity>> QueryAll() {        try {			return ApiResult.success(deptRefService.QueryAll());        } catch (Exception ex) {			LogTool.WriteError(this.getClass().toString()+ "005", ex.getMessage());            return ApiResult.error(CodeMessage.QUERY_LIST_FAILED);        }    }	}